#include<stdio.h>

int min_c(int, int);
int zz[2005][2005] = { 0 }, ans[2005][2005] = { 0 };
int main()
{
	int n = 0, m = 0, i, j;
	scanf("%d %d", &n, &m);
	for (i = 1; i <= m; i++)
	{
		for (j = 1; j <= n; j++)
		{
			scanf("%d", &zz[i][j]);
		}
		ans[i][1] = zz[i][1];
	}
	for (j = 2; j <= n; j++)
	{
		for (i = 2; i <= m; i++)
		{
			ans[i][j] = min_c(ans[i - 1][j - 1], ans[i][j - 1]) + zz[i][j];
		}
		ans[1][j] = min_c(ans[m][j - 1], ans[1][j - 1]) + zz[1][j];
	}
	int ans_min = ans[1][n];
	for (i = 2; i <= m; i++)
	{
		ans_min = min_c(ans[i][n], ans_min);
	}
	printf("%d", ans_min);
	return 0;
}
int min_c(int a, int b)
{
	return a < b ? a : b;
}
